package com.lori.search.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lori.netshop.util.NetshopResult;
import com.lori.search.mapper.ItemMapper;
import com.lori.search.pojo.Item;
import com.lori.search.service.ItemService;
@Service
public class ItemServiceimp implements ItemService{
	//注入mapper
	@Autowired
	private ItemMapper itemMapper;
	//注入solrserver
	@Autowired
	private SolrServer httpSolrServer;

	@Override
	public NetshopResult writeToSolr() {
		try {
			List<Item> itemList = itemMapper.getItemList();
			//遍历
			for(Item item:itemList){
				//创建solr文档
				SolrInputDocument document = new SolrInputDocument();
				document.addField("id", item.getId());
				document.addField("item_category_name", item.getCatname());
				document.addField("item_desc", item.getItem_desc());
				document.addField("item_image", item.getImage());
				document.addField("item_price", item.getPrice());
				document.addField("item_sell_point", item.getSell_point());
				document.addField("item_title", item.getTitle());
				httpSolrServer.add(document);
			}
			//提交
			httpSolrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return NetshopResult.build(500, "数据存入solr服务器失败");
		}
		return NetshopResult.ok();
	}
	
}
